﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using Model;

namespace BLL.ResourceManage
{
    /// <summary>
    /// 通用资源添加
    /// </summary>
    public class ResAdd
    {
        private readonly OracleDAL.ResourceManage.ResAdd dal = new OracleDAL.ResourceManage.ResAdd();
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        ///*************************************************************************
        ///函数名称:	AddResource
        ///功能描述:	添加资源信息
        ///参数列表:  
        ///参数1   :   资源基本表名
        ///参数2   :   资源基本表中需要自动编码的列，以逗号为分割符
        ///参数3   :   资源基本表中需要自动编码的列的类型（即cfg_resource_fields表中的valuetype字段值），以逗号为分割符
        ///参数4   :   资源基本表中非自动编码列的名值对，以分号为分割符
        ///参数5   :   资源基本表中非自动编码列中附件文件值所属的列号（从0开始），只处理一种附件的情况
        ///参数6   :   资源基本表中非自动编码列中列的类型（即cfg_resource_fields表中的valuetype字段值），以分号为分割符
        ///作    者:   
        ///创建日期:	2009/02/27
        ///*************************************************************************
        #region
        public  string AddResource(String resTable, String autocols, String autocolType, String unautocols, String unautocolvalue, String unautocoltype)
        {
            string strReturn;
            string strSql;
            string strAutocolValue;
            strAutocolValue = getAutoColumns(autocols, autocolType);

            if (unautocols != "")
            {
                unautocols = unautocols.Substring(0, unautocols.Length - 1);
                unautocolvalue = unautocolvalue.Substring(0, unautocolvalue.Length - 1);
            }
            unautocols = unautocols.Replace(";", ",");
            unautocolvalue = unautocolvalue.Replace(";", ",");
            autocols = autocols.Replace(";", ",");

            if (autocols != "")
            {
              
                strSql = "insert into " + resTable + "(" + autocols + "," + unautocols + ") values(" + strAutocolValue + unautocolvalue + ")";
            }
            else
            {
               
                strSql = "insert into " + resTable + "(" + unautocols + ") values(" + unautocolvalue + ")";
            }

            Model.ResourceManage.ResAdd resAdd = dal.GetExcutState(strSql);
            if (resAdd.GetResult == 0)
            {
                strReturn = "失败";
            }
            else 
            {
                strReturn = "成功";
            }
            return strReturn;

          
           // if Model.ResourceManage.ResAdd.g
        }
        #endregion
        ///*************************************************************************
        ///函数名称:	getAutoColumns
        ///功能描述:	生成自动编码相应字段的值
        ///参数列表:   
        ///参数1   :   自动编码列，以逗号为分割符
        ///参数2   :   自动编码列类型，以逗号为分割符
        ///作    者:	
        ///创建日期:	2009/02/07  16:02:30
        /// *************************************************************************
        #region
        public string getAutoColumns(String autocols, String autocoltype)
        {
            string[] colTypeArr;
            string[] colArr;
            String autoColValue="";

            colTypeArr = autocoltype.Split(';');
            colArr = autocols.Split(';');


            for (int rodItem = 0; rodItem < colTypeArr.Length; rodItem++)
            {
                switch (colTypeArr[rodItem])
               {
                   case "string$random":   //时间加三位随机数的字符串型规则
                       {
                           Random r1 = new Random();
                           autoColValue = autoColValue + "'" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + r1.Next(0, 999).ToString() + ",";
                        //   autoColValue ="'" + autoColValue + "'" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + r1.Next(0, 999).ToString() + "',";
                           break;
                       }
                   case "number$random":
                       {
                           Random r1 = new Random();
                           autoColValue = autoColValue + System.DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + r1.Next(0, 999).ToString() + ",";
                        //   autoColValue = "'" + autoColValue + System.DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + r1.Next(0, 999).ToString() + "',";
                           break;
                       }
                   case "date":
                       {
                           autoColValue = autoColValue + "sysdate,";
                           break;
                       }
                   default:
                       {
                           Random r1 = new Random();
                           autoColValue = autoColValue  + "'" + System.DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + r1.Next(0, 999).ToString() + "',";
                          // autoColValue = "'" + autoColValue + "'" + System.DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + r1.Next(0, 999).ToString() + "'',";
                           break;
                       }
               }
            }
            return autoColValue;
        }
        #endregion
    }
}
